package com.tebiecloud.order.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tebiecloud.common.utils.RedisUtils;
import com.tebiecloud.mpweixin.client.constants.QueueConstants;
import com.tebiecloud.order.client.constatns.OrderConstants;
import com.tebiecloud.order.client.model.entity.Reserve;
import com.tebiecloud.order.server.mapper.ReserveMapper;
import com.tebiecloud.order.server.mq.MqMessage;
import com.tebiecloud.order.server.service.ReserveService;
import com.tebiecloud.common.mybatis.base.service.impl.BaseServiceImpl;
import org.assertj.core.util.Lists;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageDeliveryMode;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 预约单 服务实现类
 * @author tebie
 * @date 2019-10-28
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class ReserveServiceImpl extends BaseServiceImpl<ReserveMapper, Reserve> implements ReserveService {

    @Autowired
    ReserveMapper reserveMapper;

    @Autowired
    RedisUtils redisUtils;

    @Autowired
    private AmqpTemplate amqpTemplate;

    @Autowired
    private MqMessage mqMessage;

    @Override
    public void noticeReserve(long storeId) {
        int time =  5;
        String key = OrderConstants.REDIS_RESERVER_STORE_+storeId;
        boolean isLock = redisUtils.lock(key,time);
        if(!isLock){
            throw new RuntimeException("库存被占用，请稍等");
        }
        //查询当前门店的待服务列表
        List<Integer> statusList = Lists.newArrayList();
        statusList.add(2);
        statusList.add(3);
        statusList.add(4);
        QueryWrapper<Reserve> reserveQueryWrapper = new QueryWrapper<>();
        reserveQueryWrapper.eq("store_id",storeId);
        reserveQueryWrapper.in("status",statusList);
        List<Reserve> reserveList = reserveMapper.selectList(reserveQueryWrapper);
        int tuiNum = 0;
        if(reserveList!=null && reserveList.size()>0){
            if(OrderConstants.RESERVE_NUM-reserveList.size()>=0)tuiNum = OrderConstants.RESERVE_NUM-reserveList.size();
        }
        //修改推送
        if(tuiNum>0){
            String sql = "LIMIT "+tuiNum;
            QueryWrapper<Reserve> tuiReserveQueryWrapper = new QueryWrapper<>();
            tuiReserveQueryWrapper.eq("store_id",storeId);
            tuiReserveQueryWrapper.in("status",1);
            tuiReserveQueryWrapper.last(sql);
            tuiReserveQueryWrapper.orderByAsc("number");
            List<Reserve> tuiReserveList = reserveMapper.selectList(tuiReserveQueryWrapper);
            if(tuiReserveList!=null && tuiReserveList.size()>0){
                for(Reserve reserve:tuiReserveList){
                    Reserve newReserve = new Reserve();
                    newReserve.setReserveId(reserve.getReserveId());
                    newReserve.setStatus(2);
                    reserveMapper.updateById(newReserve);
                    reserve.setUt(new Date());
                    amqpTemplate.convertAndSend(QueueConstants.QUEUE_RESERVE_EVENT_INFO, reserve);
                }
            }
        }
        redisUtils.unLock(key);
    }
}
